namespace C1.Win.C1FlexGrid.Util.Styles
{
    using C1.Win.C1FlexGrid.Util;
    using System;
    using System.Drawing;

    internal class Border
    {
        internal Brush _brush;
        internal Margins _insets;

        public Border()
        {
            this._insets = new Margins(0, 0, 0, 0);
        }

        public Border(Brush brush, int thickness)
        {
            this._insets = new Margins(0, 0, 0, 0);
            this._brush = brush;
            this._insets = new Margins(thickness, thickness, thickness, thickness);
        }

        public Border(Color color, int thickness)
        {
            this._insets = new Margins(0, 0, 0, 0);
            this._brush = new SolidBrush(color);
            this._insets = new Margins(thickness, thickness, thickness, thickness);
        }

        public Border(Brush brush, int left, int right, int top, int bottom)
        {
            this._insets = new Margins(0, 0, 0, 0);
            this._brush = brush;
            this._insets = new Margins(left, right, top, bottom);
        }

        public Border(Color color, int left, int right, int top, int bottom)
        {
            this._insets = new Margins(0, 0, 0, 0);
            this._brush = new SolidBrush(color);
            this._insets = new Margins(left, right, top, bottom);
        }

        public virtual Margins GetInsets()
        {
            return (Margins) this._insets.Clone();
        }

        public Rectangle InnerRect(Rectangle r)
        {
            Margins m = this.GetInsets();
            return StyleContext.InnerRect(r, m);
        }

        public virtual void Render(Graphics g, Rectangle r)
        {
            int width = r.Width;
            int height = r.Height;
            if (((this._brush != null) && (width > 0)) && (height > 0))
            {
                Margins margins = this.GetInsets();
                if (margins.Left > width)
                {
                    margins.Left = width;
                }
                if (margins.Right > width)
                {
                    margins.Right = width;
                }
                if (margins.Top > height)
                {
                    margins.Top = height;
                }
                if (margins.Bottom > height)
                {
                    margins.Bottom = height;
                }
                if (margins.Left > 0)
                {
                    g.FillRectangle(this._brush, r.X, r.Y, margins.Left, r.Height);
                }
                if (margins.Top > 0)
                {
                    g.FillRectangle(this._brush, r.X, r.Y, r.Width, margins.Top);
                }
                if (margins.Right > 0)
                {
                    g.FillRectangle(this._brush, (r.X + r.Width) - margins.Right, r.Y, margins.Right, r.Height);
                }
                if (margins.Bottom > 0)
                {
                    g.FillRectangle(this._brush, r.X, (r.Y + r.Height) - margins.Bottom, r.Width, margins.Bottom);
                }
            }
        }
    }
}

